package com.lm.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lm.service.BlogService;
import com.lm.service.CommentService;
import com.lm.service.LectureService;
import com.lm.service.UserService;
import com.lm.result.MsgResult;
import com.lm.pojo.User;
import com.lm.pojo.UserInfo;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/article")
public class ArticleController {

    private static final Logger logger = LoggerFactory.getLogger(ArticleController.class);

    @Autowired
    private BlogService blogService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private LectureService lectureService;

    @Autowired
    private UserService userService;

    /**
     * 管理员删除文章
     */
    @PostMapping("/admin/deleteArticle/{bid}")
    @ResponseBody
    public MsgResult adminDeleteArticle(@PathVariable("bid") String bid, HttpSession session) {
        try {
            // 检查是否是管理员
            User loginUser = (User) session.getAttribute("loginUser");
            if (loginUser == null || loginUser.getRoleId() != 1) {
                return MsgResult.fail().add("msg", "无权限执行此操作");
            }
            
            // 删除文章
            blogService.deleteArticle(bid);
            return MsgResult.success().add("msg", "文章删除成功");
        } catch (Exception e) {
            logger.error("删除文章失败", e);
            return MsgResult.fail().add("msg", "删除文章失败：" + e.getMessage());
        }
    }

    /**
     * 管理员删除讲座
     */
    @PostMapping("/admin/deleteLecture/{lid}")
    @ResponseBody
    public MsgResult adminDeleteLecture(@PathVariable("lid") Long lid, HttpSession session) {
        try {
            User loginUser = (User) session.getAttribute("loginUser");
            if (loginUser == null || loginUser.getRoleId() != 1) {
                return MsgResult.fail().add("msg", "无权限执行此操作");
            }

            // 删除讲座相关的所有评论
            commentService.deleteCommentsByLectureId(lid);
            
            // 删除讲座
            lectureService.deleteLecture(lid);
            
            return MsgResult.success().add("msg", "讲座删除成功");
        } catch (Exception e) {
            logger.error("删除讲座失败: {}", e.getMessage());
            return MsgResult.fail().add("msg", "删除讲座失败");
        }
    }

    /**
     * 管理员删除评论
     */
    @PostMapping("/admin/deleteComment/{cid}")
    @ResponseBody
    public MsgResult adminDeleteComment(@PathVariable("cid") String cid, HttpSession session) {
        try {
            User loginUser = (User) session.getAttribute("loginUser");
            if (loginUser == null || loginUser.getRoleId() != 1) {
                return MsgResult.fail().add("msg", "无权限执行此操作");
            }

            commentService.delCommentByCid(cid);
            return MsgResult.success().add("msg", "评论删除成功");
        } catch (Exception e) {
            logger.error("删除评论失败: {}", e.getMessage());
            return MsgResult.fail().add("msg", "删除评论失败");
        }
    }
} 